/**
 * Copyright © 2011 Software in Life Inc. All rights reserved.
 */
package com.softwareinlife.snaf.user.control;

import java.util.List;
import java.util.logging.Logger;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;

import com.softwareinlife.snaf.user.dao.UserDao;
import com.softwareinlife.snaf.user.model.UserModel;
import com.softwareinlife.snaf.user.service.UserService;

/**
 * <b>Front Controller for User</b>
 * 
 * <p>
 * For a complete reference see <a href=
 * "http://static.springsource.org/spring/docs/3.1.0.M1/spring-framework-reference/html/mvc.html"
 * > Spring Framework Reference Documentation</a>
 * </p>
 * 
 * @author Jang, Sun-Jin(jangsunjin@softwareinlife.com)
 */
@Controller
@RequestMapping("/user")
public class UserController {

	/**
	 * Logger
	 */
	protected static Logger logger = Logger.getLogger("controller");
	
	/**
	 * DAO
	 */
	@Autowired
	private UserDao dao;
	
	/**
	 * Service
	 */
	@Autowired
	private UserService service;
	
	/**
	 * Handles and retrieves all persons and show it in a JSP page
	 * 
	 * @return the name of the JSP page
	 */
    @RequestMapping(value = "/list", method = RequestMethod.GET)
    public ModelAndView list(HttpServletRequest req,
			HttpServletResponse res, HttpSession session) {
    	
    	// Retrieve all persons by delegating the call to dao
    	List<UserModel> models = dao.getModels();
    	
    	// This will resolve to /WEB-INF/jsp/personspage.jsp
    	return new ModelAndView("user-list", "models", models);
	}

    /**
     * Adds a new person by delegating the processing to dao.
     * Displays a confirmation JSP page
     * 
     * @return  the name of the JSP page
     */
    @RequestMapping(value = "/add", method = RequestMethod.POST)
    public ModelAndView add(@ModelAttribute("personAttribute") UserModel model) {
    	// The "personAttribute" model has been passed to the controller from the JSP
    	// We use the name "personAttribute" because the JSP uses that name
		
		// Call dao to do the actual adding
		dao.create(model);

    	// Retrieve all persons by delegating the call to dao
    	List<UserModel> models = dao.getModels();
    	
    	// This will resolve to /WEB-INF/jsp/personspage.jsp
    	return new ModelAndView("user-list", "models", models);
	}
    
    /**
     * Deletes an existing person by delegating the processing to dao.
     * Displays a confirmation JSP page
     * 
     * @return  the name of the JSP page
     */
    @RequestMapping(value = "/delete", method = RequestMethod.GET)
    public ModelAndView delete(@RequestParam(value="id", required=true) String seq, 
    										Model model) {
   		// Call dao to do the actual deleting
		dao.delete(seq);
		// Add id reference to Model
		model.addAttribute("seq", seq);
    	
    	// Retrieve all persons by delegating the call to dao
    	List<UserModel> models = dao.getModels();
    	
    	// This will resolve to /WEB-INF/jsp/personspage.jsp
    	return new ModelAndView("user-list", "models", models);
	}
    
    /**
     * Retrieves the edit page
     * 
     * @return the name of the JSP page
     */
    @RequestMapping(value = "/update", method = RequestMethod.GET)
    public String getEdit(@RequestParam(value="id", required=true) String id,  
    										Model model) {
    	// Retrieve existing Person and add to model
    	// This is the formBackingOBject
    	model.addAttribute("personAttribute", dao.read(id));
    	
    	// This will resolve to /WEB-INF/jsp/editpage.jsp
    	return "user-edit";
	}
    
    /**
     * Edits an existing person by delegating the processing to dao.
     * Displays a confirmation JSP page
     * 
     * @return  the name of the JSP page
     */
    @RequestMapping(value = "/update", method = RequestMethod.POST)
    public ModelAndView saveEdit(@ModelAttribute("personAttribute") UserModel person, 
    										   @RequestParam(value="userId", required=true) String userId, 
    												Model model) {
    	// The "personAttribute" model has been passed to the controller from the JSP
    	// We use the name "personAttribute" because the JSP uses that name
    	
    	// We manually assign the id because we disabled it in the JSP page
    	// When a field is disabled it will not be included in the ModelAttribute
    	person.setUserId(userId);
    	
    	// Delegate to dao for editing
    	dao.update(person);
    	
    	// Add id reference to Model
		model.addAttribute("userId", userId);
		
    	// Retrieve all persons by delegating the call to dao
    	List<UserModel> models = dao.getModels();
    	
    	// This will resolve to /WEB-INF/jsp/personspage.jsp
    	return new ModelAndView("user-list", "models", models);
	}
}
